package dice.sinanya.dice.game;

import dice.sinanya.db.custom.InsertCustomSearch;
import dice.sinanya.db.custom.SelectCustomSearch;
import dice.sinanya.entity.EntityCustomSearch;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.CustomAlreadyExsitException;
import dice.sinanya.exceptions.CustomFormatException;

import static dice.sinanya.system.MessagesTag.TAG_CUSTOM_SEARCH_GET;
import static dice.sinanya.system.MessagesTag.TAG_CUSTOM_SEARCH_INPUT;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entitySystemProperties;
import static dice.sinanya.tools.getinfo.GetNickName.getTrueNickName;
import static dice.sinanya.tools.getinfo.GetTime.getNowString;
import static dice.sinanya.tools.getinfo.GetTime.getTime;
import static dice.sinanya.tools.makedata.MakeMessages.deleteTag;
import static dice.sinanya.tools.makedata.Sender.sender;

public class CustomSearch {
    private final EntityTypeMessages entityTypeMessages;

    public CustomSearch(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public void inputCustomSearch() throws CustomFormatException, CustomAlreadyExsitException {
        String tag = TAG_CUSTOM_SEARCH_INPUT;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));

        if (!msg.contains("|")) {
            throw new CustomFormatException(entityTypeMessages);
        }

        SelectCustomSearch selectCustomSearch = new SelectCustomSearch();
        InsertCustomSearch insertCustomSearch = new InsertCustomSearch();
        String key = msg.split("\\|")[0];
        String messages = msg.split("\\|")[1];
        String authorId = entityTypeMessages.getFromQqString();

        if (!selectCustomSearch.checkCanInput(key, authorId)) {
            throw new CustomAlreadyExsitException(entityTypeMessages);
        }

        EntityCustomSearch entityCustomSearch = new EntityCustomSearch();

        entityCustomSearch.setInputDate(getTime(getNowString()));
        entityCustomSearch.setKey(key);
        entityCustomSearch.setMessages(messages);
        entityCustomSearch.setAuthorId(authorId);
        entityCustomSearch.setAuthorNick(getTrueNickName(entityTypeMessages));

        insertCustomSearch.insertCustom(entityCustomSearch);

        if (selectCustomSearch.checkOverWrite(key, authorId)) {
            sender(entityTypeMessages, entitySystemProperties.getCustomSearchUpdate(entityTypeMessages.getFromGroupString()));
        } else {
            sender(entityTypeMessages, entitySystemProperties.getCustomSearchInsert(entityTypeMessages.getFromGroupString()));
        }
    }

    public void getCustomSearch() {
        String tag = TAG_CUSTOM_SEARCH_GET;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        SelectCustomSearch selectCustomSearch = new SelectCustomSearch();
        sender(entityTypeMessages, selectCustomSearch.getCustomMessages(msg));
        System.gc();
    }
}
